import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:url_launcher/url_launcher.dart';
import 'ads/consent_manager.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';


class SettingPage extends StatefulWidget {
  const SettingPage({
    super.key,
  });

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  var _isPrivacyOptionsRequired = false;

  void _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'not to : $url';
    }
  }

  void _getIsPrivacyOptionsRequired() async {
    if (await ConsentManager.instance.isPrivacyOptionsRequired()) {
      setState(() {
        _isPrivacyOptionsRequired = true;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    _getIsPrivacyOptionsRequired();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Column(
      children: [
        Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 1,
              color: const Color.fromRGBO(0, 0, 0, .1),
              
            ),
            borderRadius: BorderRadius.circular(4)
          ),
          child: ListTile(
            leading: const Icon(Icons.privacy_tip),
            title: const Text('Privacy Policy', style: TextStyle(fontSize: 20),),
            // subtitle: Text('This is the home page'),
            // trailing: const Icon(Icons.arrow_forward),
            onTap: () {
              // 点击事件
              _launchURL('https://www.tomatocx.com/privacy-policy.html');
            },
          ),
        ),
        const SizedBox(height: 16,),
        
        if(_isPrivacyOptionsRequired )
          Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 1,
              color: const Color.fromRGBO(0, 0, 0, .1),
              
            ),
            borderRadius: BorderRadius.circular(4)
          ),
          child: ListTile(
              leading: const Icon(Icons.lock),
              title: const Text('Privacy Setting', style: TextStyle(fontSize: 20),),
              // subtitle: Text('This is the home page'),
              // trailing: Icon(Icons.arrow_forward),
              onTap: () {
                // 点击事件
                ConsentForm.showPrivacyOptionsForm((formError) {
                  if (formError != null) {
                    debugPrint(
                        "${formError.errorCode}: ${formError.message}");
                  }
                });
              },
            )
          ),

        InkWell(
          onTap: () {
             _launchURL('https://apps.apple.com/us/app/flower-name-diy-wallpaper/id6720739147');
          },
          child:
        Container(
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4),
            border: Border.all(width: 1, color: const Color.fromRGBO(0, 0, 0, .1),)
          ),
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    width: 60,
                    height: 60,
                    decoration: BoxDecoration(
                      image: const DecorationImage(
                          image: AssetImage('images/flower/icon.png'),
                          fit: BoxFit.fitWidth),
                      borderRadius: BorderRadius.circular(12),
                      // color: const Color.fromARGB(8, 0, 0, 0),
                    ),
                  ),
                  const SizedBox(width: 10,),
                  const Expanded(child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Flower Name: DIY Wallpaper', style: TextStyle(fontFamily: 'OpenSans', fontSize: 16, fontWeight: FontWeight.bold),),
                      Text('Create Unique Bouquet to your loved ones.', style: TextStyle(fontFamily: 'OpenSans',),)
                    ],
                  )),
                  Container(
                    width: 60,
                    height: 36,
                    decoration: BoxDecoration(
                      color: const Color.fromARGB(255, 215, 31, 39),
                      borderRadius: BorderRadius.circular(20)
                    ),
                    child: const Center(
                      child: Text('GET', style: TextStyle(color: Colors.white),),
                    ) ,
                  )
                ],
              ),
              const SizedBox(height: 12,),
              Row(
                children: [
                  Expanded(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(12), // 使用 ClipRRect 来处理圆角
                      child: Container(
                        child: Image.asset(
                          'images/flower/2.png',
                          fit: BoxFit.cover, // 确保图片填充整个容器
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 12,),
                  Expanded(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(12), // 使用 ClipRRect 来处理圆角
                      child: Container(
                        child: Image.asset(
                          'images/flower/3.png',
                          fit: BoxFit.cover, // 确保图片填充整个容器
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 12,),
                  Expanded(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(12), // 使用 ClipRRect 来处理圆角
                      child: Container(
                        child: Image.asset(
                          'images/flower/4.png',
                          fit: BoxFit.cover, // 确保图片填充整个容器
                        ),
                      ),
                    ),
                  ),
                ],
              )
            ],
          ),
        )
         ,
        )
      ],
    ));
  }
}